// 五步
import {IndexModel} from "../../api/index"
const indexModel = new IndexModel()
// 第八步
import {helper} from "../../utils/helper"

Page({
  data: {
    image: [
      {
        id: 1,
        img: "//img14.360buyimg.com/pop/s1180x940_jfs/t1/195610/31/12147/83154/60e557fbEef73a68a/5a9ddcbc508bb8b5.jpg.webp"
      },
      {
        id: 2,
        img: "https://img1.baidu.com/it/u=1021698570,4055706443&fm=26&fmt=auto&gp=0.jpg"
      },
      {
        id: 3,
        img: "https://img1.baidu.com/it/u=1271607131,1035620186&fm=26&fmt=auto&gp=0.jpg"
      },
      {
        id: 4,
        img: "https://img2.baidu.com/it/u=1539068888,918756408&fm=26&fmt=auto&gp=0.jpg"
      }
    ],
    indicatorDots: true,
    autoplay: true,
    circular: true,
    interval: 3000,
    duration: 500,
  },
  // 四步
  shopCart(){
   wx.scanCode({
     onlyFromCamera: true,
     success:(res)=>{
       const {result} =res
      //  console.log(result);
      this.getShpCart(result)
     },
     fail: ()=>{
      wx.showToast({
        title: '扫码失败', 
      });
     },
   });
  },
  // 六步 获取商品信息方法  result条形码
  getShpCart(result){
    indexModel.getProduction(result).then((res)=>{
      // console.log(res);
      if(res.data.result){
        // 调用添加数据到购物车方法
        helper.addCar(res.data.result[0])
        // 跳转到购物车页面
        wx.navigateTo({
          url: '/pages/carts/carts',
        });
      }

    }).catch((error)=>{
      console.log(error);
    })
  },

  onLoad: function (options) {

  },

})